package com.vito.dcs.sys.sysLayer.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.exception.ParamErrorException;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.dcs.sys.layer.model.SysLayerModel;
import com.vito.dcs.sys.layer.service.SysLayerService;

/**
 * 结构层
 * @author lyhao
 *
 */
@Controller
@RequestMapping("/dcs/sys/layer")
public final class SysLayerAction extends BaseAction {
	
	private static final Logger log = LoggerFactory.getLogger(SysLayerAction.class);
	
	@Reference
	private SysLayerService sysLayerServiceImpl;
	
	/**
	 * 保存
	 * @param sysLayerModel
	 * @return
	 */
	@RequestMapping("saveSysLayer")
	@ResponseBody
	public Result saveSysLayer(@RequestBody SysLayerModel sysLayerModel){
		Result res = new Result();
        UserModel user = SessionMap.getObj();
        if(null==user){
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(ResMsg.MSG_LOGIN_NEED);
            return res;
        }
		try{
			Validata.isNullOrEmpty(sysLayerModel, "结构层表不能为空");
			Validata.isNullOrEmpty(sysLayerModel.getLayerName(), "结构层名称不能为空");
			sysLayerModel.setUpdateUser(user.getUserId());
			sysLayerServiceImpl.saveSysLayer(sysLayerModel);
			res.setCode(Result.CODE_OK);
			res.setMsg(ResMsg.SAVESUC);
		}catch(ParamErrorException e){
			res.setMsg(e.getMessage());
			res.setCode(Result.CODE_ERROR);
		}catch(Exception ex){
			log.error("保存失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	/**
	 * 分页查询
	 * @param sysLayerModel
	 * @return
	 */
	@RequestMapping("querySysLayerByPage")
	@ResponseBody
	public Result querySysLayerByPage(SysLayerModel sysLayerModel){
		Result res = new Result();
        UserModel user =SessionMap.getObj();
        if(null==user){
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(ResMsg.MSG_LOGIN_NEED);
            return res;
        }
		try{
			res.setData(sysLayerServiceImpl.querySysLayerByPage(sysLayerModel));
			res.setCode(Result.CODE_OK);
		}catch(Exception ex){
			log.error("查询失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	/**
	 * 查询
	 * @param sysLayerModel
	 * @return
	 */
	@RequestMapping("querySysLayer")
	@ResponseBody
	public Result querySysLayer(SysLayerModel sysLayerModel){
		Result res = new Result();
        UserModel user =SessionMap.getObj();
        if(null==user){
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(ResMsg.MSG_LOGIN_NEED);
            return res;
        }
		try{
			res.setData(sysLayerServiceImpl.querySysLayer(sysLayerModel));
			res.setCode(Result.CODE_OK);
		}catch(Exception ex){
			log.error("查询失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
		
	/**
	 * 修改
	 * @param sysLayerModel
	 * @return
	 */
	@RequestMapping("updateSysLayer")
	@ResponseBody
	public Result updateSysLayer(@RequestBody SysLayerModel sysLayerModel){
		Result res = new Result();
        UserModel user =SessionMap.getObj();
        if(null==user){
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(ResMsg.MSG_LOGIN_NEED);
            return res;
        }
		try{
			Validata.isNullOrEmpty(sysLayerModel.getId(),"主键ID不能为空");
			Validata.isNullOrEmpty(sysLayerModel.getLayerName(), "结构层名称不能为空");
			sysLayerModel.setUpdateUser(user.getUserId());
			res.setData(sysLayerServiceImpl.updateSysLayer(sysLayerModel));
			res.setCode(Result.CODE_OK);
			res.setMsg(ResMsg.UPDSUC);
		}catch(ParamErrorException e){
			res.setMsg(e.getMessage());
			res.setCode(Result.CODE_ERROR);
		}catch(Exception ex){
			log.error("更新失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	/**
	 * 删除
	 * @param sysLayerModel
	 * @return
	 */
	@RequestMapping("deleteSysLayer")
	@ResponseBody
	public Result deleteSysLayer(SysLayerModel sysLayerModel){
		Result res = new Result();
        UserModel user =SessionMap.getObj();
        if(null==user){
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(ResMsg.MSG_LOGIN_NEED);
            return res;
        }
		try{
			Validata.isNullOrEmpty(sysLayerModel.getId(), "所选项不能为空");
			res.setData(sysLayerServiceImpl.deleteSysLayer(sysLayerModel));
			res.setCode(Result.CODE_OK);
			res.setMsg(ResMsg.DELCOM);
		}catch(ParamErrorException e){
			res.setMsg(e.getMessage());
			res.setCode(Result.CODE_ERROR);
		}catch(Exception ex){
			log.error("删除失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	/**
	 * 查询结构层和材料类型集合
	 * @return
	 */
	@RequestMapping("queryLayerAndMaterialList")
	@ResponseBody
	public Result queryLayerAndMaterialList(){
		Result rs = new Result();
		try {
			rs.setCode(Result.CODE_OK);
			rs.setData(sysLayerServiceImpl.queryLayerAndMaterialList());
		} catch (Exception e) {
			log.error("查询失败",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
	
	/**
	 * 查询所有面层集合
	 * @param sysLayerModel
	 * @return
	 */
	@RequestMapping("querySysLayerList")
	@ResponseBody
	public Result querySysLayerList(SysLayerModel sysLayerModel){
		Result rs = new Result();
		try {
			rs.setCode(Result.CODE_OK);
			rs.setData(sysLayerServiceImpl.querySysLayerList(sysLayerModel));
		} catch (Exception e) {
			log.error("查询失败",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
	
}



